﻿namespace MyManagement.Model
{
    using System;
    using System.Linq;
    using RS = Resources;

    /// <summary>
    /// An entry in the backlog.
    /// </summary>
    public class BacklogItem
    {
        private Backlog parent;

        /// <summary>
        /// Initializes a new instance of the <see cref="BacklogItem" /> class.
        /// </summary>
        public BacklogItem()
        {
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public string Id
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the category.
        /// </summary>
        /// <value>The category.</value>
        public BacklogItemCategory Category
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the product.
        /// </summary>
        /// <value>The product.</value>
        public Product Product
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the iteration.
        /// </summary>
        /// <value>The iteration.</value>
        public Iteration Iteration
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the body.
        /// </summary>
        /// <value>The body.</value>
        public string Body
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the estimate.
        /// </summary>
        /// <value>The estimate.</value>
        public decimal Estimate
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the detail estimate.
        /// </summary>
        /// <value>The detail estimate.</value>
        public decimal DetailEstimate
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the completed work.
        /// </summary>
        /// <value>The completed work.</value>
        public decimal CompletedWork
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the remaining work.
        /// </summary>
        /// <value>The remaining work.</value>
        public decimal RemainingWork
        {
            get;
            set;
        }

        /// <summary>
        /// Runs the validation rules to make sure the object is correctly setup and throws an exception if not.
        /// </summary>
        public void EnsureValid()
        {
            if (string.IsNullOrEmpty(this.Id))
            {
                throw new InvalidOperationException(string.Format(RS.PropertyValueIsRequired, "Id"));
            }

            if (string.IsNullOrEmpty(this.Title))
            {
                throw new InvalidOperationException(string.Format(RS.PropertyValueIsRequired, "Title"));
            }
        }

        internal void ApplyDefaultValues()
        {
            if (this.parent == null)
            {
                throw new InvalidOperationException(RS.BacklogInstanceIsRequired);
            }

            if (this.Product == null && parent.DefaultProduct != null)
            {
                this.Product = parent.DefaultProduct;
            }

            if (this.Project == null && parent.DefaultProject != null)
            {
                this.Project = parent.DefaultProject;
            }

            if (this.Category == null && parent.DefaultCategory != null)
            {
                this.Category = parent.DefaultCategory;
            }
        }

        internal void ChangeBacklog(Backlog value)
        {
            this.parent = value;
        }
    }
}